//
//  PACACalendarView.m
//  ICrEAM
//
//  Created by rainimute on 16/12/22.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "PACACalendarView.h"
#import "CalendarView.h"

@interface PACACalendarView ()<CalendarDelegate>

@property (nonatomic, strong) CalendarView *calendarView;

@property (nonatomic, strong) UIButton *cancelButton;

@property (nonatomic, strong) UIButton *confirmButton;

@end

@implementation PACACalendarView

- (instancetype)initCalendarViewWithDidSelectedDate:(NSDate *)didSelectedDate minDate:(NSDate *)minDate maxDate:(NSDate *)maxDate andFrame:(CGRect)frame{
    if (self = [super initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)]) {
        self.backgroundColor = [UIColor clearColor];
        UIView *backView = [[UIView alloc]initWithFrame:self.frame];
        backView.backgroundColor = [UIColor clearColor];
        [self addSubview:backView];
        UIView *contentView = [[UIView alloc]initWithFrame:frame];
        contentView.backgroundColor = TextFeildBackgroundColor_OffWhite;
        
        //增加取消手势
        UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelSelect)];
        backView.userInteractionEnabled = YES;
        [backView addGestureRecognizer:tapGr];
        
        //
        self.cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 44)];
        [self.cancelButton setUpButtonWithNormolTitle:@"清空" selectedTitle:@"清空" normolTitleSize:16 selectedTitleSize:16 normolTitleColor:[UIColor redColor] selectedTitleColor:[UIColor redColor] normolBcakImage:nil selectedBackImage:nil actionSEL:@selector(cancelButtonAction:) target:self];
        [contentView addSubview:self.cancelButton];
        //
        self.confirmButton = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetWidth(frame)-self.cancelButton.width, 0, self.cancelButton.width, self.cancelButton.height)];
        [self.confirmButton setUpButtonWithNormolTitle:@"确定" selectedTitle:@"确定" normolTitleSize:16 selectedTitleSize:16 normolTitleColor:[UIColor blackColor] selectedTitleColor:[UIColor blackColor] normolBcakImage:nil selectedBackImage:nil actionSEL:@selector(confirmButtonAction:) target:self];
        [contentView addSubview:self.confirmButton];
        
        self.calendarView = [CalendarView createCalendarViewWithDidSelectedDate:didSelectedDate minDate:minDate maxDate:maxDate andFrame:CGRectMake(0, 44, contentView.width, contentView.height-54)];
        [contentView addSubview:self.calendarView];
        
        [self addSubview:contentView];
        
    }
    return self;
}

- (void)cancelSelect
{
    [self removeFromSuperview];
}

- (void)cancelButtonAction:(UIButton*)sender{
    //清空原来的时间
    if([self.delegate respondsToSelector:@selector(delTime:)]){
        [self.delegate delTime:self];
    }
    [self removeFromSuperview];
}

- (void)confirmButtonAction:(UIButton*)sender{
    NSDate *selectedDate = self.calendarView.didSelectedDate;
    if ([self.delegate respondsToSelector:@selector(calendarView:didSelectedDate:)]) {
        [self.delegate calendarView:self didSelectedDate:selectedDate];
    }
    [self removeFromSuperview];
}

+ (instancetype)createCalendarViewWithDidSelectedDate:(NSDate *)didSelectedDate minDate:(NSDate *)minDate maxDate:(NSDate *)maxDate andFrame:(CGRect)frame{
    return [[PACACalendarView alloc]initCalendarViewWithDidSelectedDate:didSelectedDate minDate:minDate maxDate:maxDate andFrame:frame];
}

@end







